package com.eascs.web.o2o.web.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.vo.QueryCustomerVO;
import com.eascs.report.common.entity.ReportStatForm;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.o2o.web.form.LoanForProvinceForm;
import com.eascs.web.o2o.web.service.BuildEmailService;
import com.eascs.web.o2o.web.service.BuildExcelService;
import com.eascs.web.o2o.web.service.ReportStatService;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/reportRest")
public class MonReportRestController  extends BaseRestController {
	@Autowired
	ReportStatService reportStatService;
	@Autowired
	BuildExcelService buildExcelService;
	@Autowired
	BuildEmailService buildEmailService;
	
	
	@Reference
	SysOrgService sysOrgService;
	
 //根据平台查询报表
  	@RequestMapping("/o2oPlatform")
  	public PaginationRestResult o2oPlatform(ReportStatForm entity) {
  		String o2ourl = "/reportstat/o2oPlatform";
  		
  		
  		if (entity != null) {
  			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
  			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
  			String province = entity.getProvince() == null ? "" : entity.getProvince();
  			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
  			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
  			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
  			o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = reportStatService.getReportStatByPlatformByPage(entity);
  		
  		//List<Object[]> data  = pageResult.getData();
  		String titles[] = { "经办平台","省区", "新增贷款准入提报", "贷款准入结束新增","内审新提交","内审结束新增","银行送审新增"};
  		SelectDataOption[] selectDataOptions = { null, null,null, null, null, null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(false);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null, null ,null});
  		o2oPagingInfo.setRendTo("#o2o_result");
  		o2oPagingInfo.setBuildRowNumber(true);
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
  	
  //根据经办人查询报表
  	@RequestMapping("/o2oOperator")
  	public PaginationRestResult o2oOperator(ReportStatForm entity) {
  		String o2ourl = "/reportstat/o2oOperator";
  		
  		
  		if (entity != null) {
  			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
  			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
  			String province = entity.getProvince() == null ? "" : entity.getProvince();
  			String platform = entity.getPlatform() == null ? "" : entity.getPlatform();
  			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
  			String operatorName = entity.getOperatorName() == null ? "" : entity.getOperatorName();
  			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
  			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
  			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
 			o2ourl = UrlUtil.appendUrl(o2ourl, "platform", platform);
 			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
 			o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
  			o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = reportStatService.getReportStatByOperatorByPage(entity);
  		
  		//List<Object[]> data  = pageResult.getData();
  		String titles[] = { "经办人","省区", "新增征信准入", "征信准入结束新增","内审新提交","内审结束新增","银行送审新增"};	
  		SelectDataOption[] selectDataOptions = { null,null,null,null,null, null, null, null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(false);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setFormats(new Format[] { null,null,null, null, null, null, null });
  		o2oPagingInfo.setRendTo("#o2o_result");
  		o2oPagingInfo.setBuildRowNumber(true);
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}

	// 信贷经理放款清单(添加在职状态和入职时间)
	@RequestMapping("/o2oDimission")
	public PaginationRestResult o2oDimission(ReportStatForm entity) {
		String o2ourl = "/reportstat/o2oDimission";


		if (entity != null) {
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String joinDateStart   = entity.getLoanDateEnd() == null ? null : entity.getJoinDateStart();
			String joinDateEnd   = entity.getLoanDateEnd() == null ? null : entity.getJoinDateEnd();
			String province      = entity.getProvince() == null ? "" : entity.getProvince();
			String platform      = entity.getPlatform() == null ? "" : entity.getPlatform();
			String ltd           = entity.getLtd() == null ? "" : entity.getLtd();
			String isDimission           = entity.getIsDimission() == null ? "" : entity.getIsDimission();
			String operatorName  = entity.getOperatorName() == null ? "" : entity.getOperatorName();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "joinDateStart", joinDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "joinDateEnd", joinDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "platform", platform);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "isDimission", isDimission);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10000);
		PageResult<Object[]> pageResult = reportStatService.getReportStatByDimissionByPage(entity);

		//List<Object[]> data  = pageResult.getData();
		String titles[] = { "信贷经理","省区","入职时间", "入职状态",  "新增征信准入", "征信准入结束新增","内审新提交","内审结束新增","银行送审新增"};
		SelectDataOption[] selectDataOptions = { null,null,null,null,null,null,null, null, null, null, null, null};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(false);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFormats(new Format[] { null,null,new SimpleDateFormat("yyyy-MM-dd"),null,null, null, null, null, null });
		o2oPagingInfo.setRendTo("#o2o_result");
		o2oPagingInfo.setBuildRowNumber(true);
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
  	
 	//根据放款银行查询报表
 	@RequestMapping("/o2oBankreFund")
 	public PaginationRestResult o2oBankreFund(ReportStatForm entity) {
 		String o2ourl = "/reportstat/o2oBankreFund";
 		
 		
 		if (entity != null) {
 			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
 			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
 			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
 			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
 			o2ourl = o2ourl + "&o2oIndex=";
 		} else {
 			o2ourl = o2ourl + "?o2oIndex=";
 		}
 		entity.setPageSize(10000);
 		PageResult<Object[]> pageResult = reportStatService.getReportStatByBankreFundByPage(entity);
 		
 		//List<Object[]> data  = pageResult.getData();
 		String titles[] = { "放款银行",  "新增贷款准入提报", "贷款准入结束新增","内审新提交","内审结束新增","银行送审新增"};
 		SelectDataOption[] selectDataOptions = { null,null,null, null, null, null, null, null};
 		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
 		o2oPagingInfo.setBuildPagingNavigation(false);
 		o2oPagingInfo.setFirstRenderColumnIndex(0);
 		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
 		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null ,null});
 		o2oPagingInfo.setRendTo("#o2o_result");
 		o2oPagingInfo.setBuildRowNumber(true);
 		ViewContext.put("vo", entity);
 		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
 	}
 	 	
 	 //根据省区查询报表
 	@RequestMapping("/o2oProvince")
 	public PaginationRestResult o2oProvince(ReportStatForm entity) {
 		String o2ourl = "/reportstat/o2oProvince";
 		
 		
 		if (entity != null) {
 			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
 			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
 			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
 			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
 			o2ourl = o2ourl + "&o2oIndex=";
 		} else {
 			o2ourl = o2ourl + "?o2oIndex=";
 		}
 		entity.setPageSize(10000);
 		PageResult<Object[]> pageResult = reportStatService.getReportStatByProvinceByPage(entity);
 		
 		//List<Object[]> data  = pageResult.getData();
 		String titles[] = { "省区",  "新增贷款准入提报", "贷款准入结束新增","内审新提交","内审结束新增","银行送审新增"};
 		SelectDataOption[] selectDataOptions = { null,null,null, null, null, null, null, null};
 		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
 		o2oPagingInfo.setBuildPagingNavigation(false);
 		o2oPagingInfo.setFirstRenderColumnIndex(0);
 		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
 		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null ,null});
 		o2oPagingInfo.setRendTo("#o2o_result");
 		o2oPagingInfo.setBuildRowNumber(true);
 		ViewContext.put("vo", entity);
 		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
 	}
 	
 	@RequestMapping(value = "/searchUnit")
	public BaseRestResult<List<Object[]>> searchUnit(QueryCustomerVO vo) {
		List<Integer> levelList =new ArrayList<Integer>();
		levelList.add(4);levelList.add(9);
		ModelResult<List<Object[]>> model =sysOrgService.getOrgListLikeNameWithLevels(vo.getOrg_name(), levelList, UserHolder.getUser());
		//ModelResult<List<Object[]>> model =  sysOrgService.getOrgListLikeName(vo.getOrg_name(), ActOrgEntity.ORG_LEVEL_PLATFORM,UserHolder.getUser());
		return BaseRestResult.buildRestResult(model.getModel());
	}
 	
 	
 	
 	@RequestMapping(value="/searchLoanByProvince")
	public PaginationRestResult searchLoanByProvince(LoanForProvinceForm entity) {
		String o2ourl = "/reportRest/searchLoanByProvince";
		if(entity != null) {
			String loanDateStart = entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd();
			String province = entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10000);
		PageResult<Object[]> pageResult = reportStatService.getLoanAmountByProvince(entity);
	 		
	 	//List<Object[]> data  = pageResult.getData();
	 	String titles[] = { "省区",  "累计放款（万元）"};
	 	SelectDataOption[] selectDataOptions = { null,null};
	 	PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
	 	o2oPagingInfo.setBuildPagingNavigation(false);
	 	o2oPagingInfo.setFirstRenderColumnIndex(0);
	 	o2oPagingInfo.setSelectDataOptions(selectDataOptions);
	 	o2oPagingInfo.setFormats(new Format[] { null, new DecimalFormat(",##0.00")});
	 	o2oPagingInfo.setRendTo("#o2o_result");
	 	o2oPagingInfo.setBuildRowNumber(true);
	 	ViewContext.put("vo", entity);
	 	return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
 	 	 	
}
